﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Forms;

namespace PedesTrainProject.Core
{
    static class Calculations
    {

        public static Vector calculateTileCoordinates(Vector position, PictureBox pb)
        {
            position.X /= pb.Width/4;
            position.Y /= pb.Height/4;
            return position;
        }

        public static Vector calculateTileCoordinates(Vector position)
        {
            int posX = (int)(position.X/100);
            int posY = (int)(position.Y/100);
            return new Vector(posX,posY);
        }

        public static Vector calculateCarCoordinates(Grid.Tile tile, int tile_index, int lane_index)
        {
            Vector position = new Vector();
            Grid.Directions direction = tile.Road.Lanes[lane_index].Direction;

            if (direction == Grid.Directions.North)
            {
                position.X = gridPosition(tile_index).X * 100 + 56;
                position.Y = 0;
            }
            else if (direction == Grid.Directions.East)
            {
                position.X = 0;
                position.Y = gridPosition(tile_index).Y * 100 + 56;
            }
            else if (direction == Grid.Directions.South)
            {
                position.X = gridPosition(tile_index).X * 100 + 34;
                position.Y = 400;
            }
            else if (direction == Grid.Directions.West)
            {
                position.X = 400;
                position.Y = gridPosition(tile_index).Y * 100 + 34;
            }

            return position;
        }

        public static Vector calculateCarCoordinates(Vector _position, Grid.Directions direction)
        {
            Vector position = _position;
            if (direction == Grid.Directions.North) position.Y -= 20;
            else if (direction == Grid.Directions.East) position.X += 20;
            else if (direction == Grid.Directions.South) position.Y += 20;
            else if (direction == Grid.Directions.West) position.X -= 20;
            return position;
        }

        public static KeyValuePair<Vector, bool> calculateCarCoordinates(Vector _position, Grid.Directions direction, int car_speed)
        {
            Vector position = _position;
            bool exit = false;
          
            if (direction == Grid.Directions.North)
            {
                position.Y -= car_speed;
                int _hundreds = (int)(_position.Y / 100);
                int hundreds = (int)(position.Y / 100);
                _hundreds *= 100;
                hundreds *= 100;
                for (int i = 0; i < 6; i++)
                {
                    if (((((position.Y)) < (hundreds + (100 * i / 6))) && (((_position.Y)) >= (hundreds + (100 * i / 6)))) || (position.Y < 0) || (position.Y > 400))
                    {
                    
                        exit = true;
                        break;
                    }
                }
            }
            else if (direction == Grid.Directions.East)
            {
                position.X += car_speed;
                int _hundreds = (int)(_position.X / 100);
                int hundreds = (int)(position.X / 100);
                _hundreds *= 100;
                hundreds *= 100;
               // System.Console.WriteLine(_hundreds + " hundreds " + "PositionXNEW:" + position.X + "PositionXOLD: " + _position.X);
                for (double i = 0; i < 6; i++)
                {
                   // System.Console.WriteLine((_hundreds*100 + (i * (100.0 / 6.0))) + "is between " + "PositionXNEW:" + position.X + "PositionXOLD: " + _position.X);
                   // if (((((_position.X) % 100) < (100 * i / 6)) && (((position.X) % 100) >= (100 * i / 6))) || (position.X < 0) || (position.X > 400))
                    if (((((_position.X)) < (hundreds + (100 * i / 6))) && (((position.X)) >= (hundreds+(100 * i / 6)))) || (position.X < 0) || (position.X > 400))
                    {
                      
                        exit = true;
                        break;
                    }
                }
            }
            else if (direction == Grid.Directions.South)
            {
                position.Y += car_speed;
                int _hundreds = (int)(_position.Y / 100);
                int hundreds = (int)(position.Y / 100);
                _hundreds *= 100;
                hundreds *= 100;
                for (int i = 0; i < 6; i++)
                {
                    if (((((_position.Y)) < (hundreds + (100 * i / 6))) && (((position.Y)) >= (hundreds + (100 * i / 6)))) || (position.X < 0) || (position.X > 400))
                    {
                        exit = true;
                        break;
                    }
                }
            }
            else if (direction == Grid.Directions.West)
            {
                position.X -= car_speed;
                int _hundreds = (int)(_position.X / 100);
                int hundreds = (int)(position.X / 100);
                _hundreds *= 100;
                hundreds *= 100;
                for (int i = 0; i < 6; i++)
                {
                    if (((((position.X)) < (hundreds + (100 * i / 6))) && (((_position.X)) >= (hundreds+(100 * i / 6)))) || (position.X < 0) || (position.X > 400))
                    {
                      
                        exit = true;
                        break;
                    
                    }
                }
            }
            KeyValuePair<Vector, bool> kvp = new KeyValuePair<Vector, bool>(position, exit);
            return kvp;
        }

        public static int tileListPosition(Vector tileCoordinate)
        {
            return (int)(tileCoordinate.X + 4 * tileCoordinate.Y);
        }

        public static Vector gridPosition(int listPosition)
        {
            int x,y=0;
            if (listPosition < 4) y = 0;
            if (listPosition >= 4) y = 1;
            if (listPosition >= 8) y = 2;
            if (listPosition >= 12) y = 3;

            x = listPosition - 4 * y;

            return new Vector(x, y);
        }
    }
}
